package com.zmn.oms.zmn.business.impl.open.v2;

import com.zmn.oms.business.interfaces.notifylog.OrderNotifyLogBService;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.model.entity.work.OrderWorkLogChange;
import com.zmn.oms.zmn.business.interfaces.open.OrderChangeNotifyService;
import com.zmn.oms.zmn.business.interfaces.open.OrderNewNotifyService;
import com.zmn.oms.zmn.business.interfaces.open.v2.V2OrderStatusSyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 类描述：第2版推送订单变化通知的服务
 *
 * @author LiangHailong
 * @date 2019/12/23 18:27
 */
@Service("v2OrderChangeNotifyService")
@Slf4j
public class V2OrderChangeNotifyServiceImpl implements OrderChangeNotifyService, OrderNewNotifyService {

    @Autowired
    private V2OrderStatusSyncService v2OrderStatusSyncService;
    @Autowired
    private OrderNotifyLogBService orderNotifyLogBService;

    /**
     * 推送订单状态
     * @param orderWorkLogChange
     */
    @Override
    @SuppressWarnings("all")
    public void notifyChange(OrderWorkLogChange orderWorkLogChange) {
        int logType = orderWorkLogChange.getLogType().intValue();

        ResultDTO resultDTO = null;
        // 修改预约时间
        if (isChangeDutyTime(logType, orderWorkLogChange)) {
            resultDTO = v2OrderStatusSyncService.pushDutyTime(orderWorkLogChange);
        }
        // 派单
        else if (OrderLogConsts.ORDER_LOG_TYPE_DISTRIBUTE == logType) {
            resultDTO = v2OrderStatusSyncService.pushMasterInfo(orderWorkLogChange);
        }
        // 上门
        else if (OrderLogConsts.ORDER_LOG_TYPE_VISIT == logType) {
            resultDTO = v2OrderStatusSyncService.pushMasterVisit(orderWorkLogChange);
        }
        // 挂起
        else if (OrderLogConsts.ORDER_LOG_TYPE_ORDER_SUSPEND == logType) {
            resultDTO = v2OrderStatusSyncService.pushOrderSuspend(orderWorkLogChange);
        }
        // 取消、失败、弃单
        else if (OrderLogConsts.ORDER_LOG_TYPE_CANCEL == logType || OrderConsts.ORDER_OP_TYPE_FAIL == logType
                || OrderConsts.ORDER_OP_TYPE_DISCARD == logType) {
            resultDTO = v2OrderStatusSyncService.pushOrderCancel(orderWorkLogChange);
        }
        // 完成
        else if (OrderLogConsts.ORDER_LOG_TYPE_COMPLETE == logType) {
            resultDTO = v2OrderStatusSyncService.pushOrderComplete(orderWorkLogChange);
        }
        // 商家备注
        else if (OrderLogConsts.ORDER_LOG_TYPE_FACTORY_REMARK == logType) {
            resultDTO = v2OrderStatusSyncService.pushFactoryRemark(orderWorkLogChange);
        }

        // 保存同步日志
        if (resultDTO != null) {
            orderNotifyLogBService.saveNotifyLog(resultDTO);
        }
    }

    /**
     * 推送新单
     * @param orderWorkLogChange
     */
    @Override
    public void notifyNewOrder(OrderWorkLogChange orderWorkLogChange) {
        v2OrderStatusSyncService.pushNewOrder(orderWorkLogChange);
    }

}
